#pragma once

#include <QWidget>
#include <QObject>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QMessageBox>
#include <QList>
#include <QMenu>
#include <QScreen>
#include <QScrollArea>
#include <QMap>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QDebug>
#include <QKeyEvent>
#include "datastruct.h"

namespace Ui {
class TreeDirectoryWidget; 
}

class TreeDirectoryWidget : public QWidget
{
    Q_OBJECT

public:
    explicit TreeDirectoryWidget(QWidget *parent = nullptr);
    ~TreeDirectoryWidget();
    void initTree();
    /*
     *      创建根节点
     * text     根节点名字
     * pid      根节点ID
     * return   返回该节点ID,如果无父节点默认值为NULL
     */
    QString CreateRootLeafNode(QString name, QString pid = NULL);
    /*
     *      创建子节点(实体对象节点)
     * info     树形节点信息实体
     * pid      子节点ID
     * return   返回该节点ID
     */
    bool CreateChildLeafNode(treeItemInfo* info, QString pid = NULL);
    /*
     *      更新叶子节点
     * info     树形节点信息实体
     * return   是否更新成功
     */
	bool UpdateChildLeafNode(treeItemInfo* info);
	void setLeafNodeCheck(QString id, bool state);
	void clearTreeNode(QString id);
	void clearAllNode();
	QMap<QString, DevStationData*> GetDevDataStation() {
		return _devCodeMap;
	}
    // 初始化
    void initTreeData(QMap<int, DevStationData* >& devMap);
	//搜索框展开根节点项
	void expandParentTreeItem(QTreeWidgetItem* item);
	void search();
	void showWaiting();
	void hideWaiting();
	//图例点击按钮，展开树形节点
	void selectAndshowTreeNode(int index);
	void keyPressEvent(QKeyEvent* event);

signals:
	void sig_itemCheckChange(QString str, bool);
	void sig_itemDoubliClicked(QString str);
	void sig_itemMenuClick(QString str);

public slots:
	void menuAction(QAction* action);
	void tree_itemChanged(QTreeWidgetItem* item, int column);
	void tree_itemDoubleClicked(const QModelIndex& index);
	void tree_itemPressed(QTreeWidgetItem* item, int column);
	void btn_expandingTreeNode(bool check);

private:
    Ui::TreeDirectoryWidget *ui;
    QTreeWidget* _leftTree = nullptr;
    QTreeWidgetItem* rootTreeItem = nullptr;
    QTreeWidgetItem* LeafTreeItem = nullptr;
    QScrollArea* scroll = nullptr;
    int index = 0;
    treeItemInfo* info = nullptr;
    QMap<QString, QTreeWidgetItem*> _treeItemMap;
    QMap<QString, QTreeWidgetItem*> _treeNameMap;    //叶子节点名字
    /*QMap中
    clear功能:虽清除但key值还在
    remove:删除，key值也消失
    key和value一损俱损*/

    //当前显示器分辨率和1920*1080分辨率的比例
    double _widthScale;
    double _hightScale;
    QMenu* _menu = nullptr;
	QMap<QString, DevStationData*> _devCodeMap;

};
